/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Core.Model.Data.Type;
using Core.Model.Data.Variables;

namespace XMHEGDecoder.Builders
{
    public static class VariableBuilder
    {
        public static void Build ( XmlElement node, Variable var )
        {
            IngredientBuilder.Build( node, var );
        }

        public static BooleanVariable BuildBoolean ( XmlElement node )
        {
            BooleanVariable ret = new BooleanVariable();
            Build( node, ret );

            XmlElement v = node[ "original-value" ];
            if ( v != null )
            {
                XmlElement t = v[ "boolean" ];
                if ( t != null )
                {
                    ret.OriginalValue = DecodeUtils.ParseBool(t.InnerText);
                }
            }

            return ret;
        }

        public static IntegerVariable BuildInteger ( XmlElement node )
        {
            IntegerVariable ret = new IntegerVariable();
            Build( node, ret );

            XmlElement v = node[ "original-value" ];
            if ( v != null )
            {
                XmlElement t = v[ "integer" ];
                if ( t != null )
                    ret.OriginalValue = DecodeUtils.ParseInt( t.InnerText );
            }

            return ret;
        }

        public static OctetStringVariable BuildOctetString ( XmlElement node )
        {
            OctetStringVariable ret = new OctetStringVariable();
            Build( node, ret );

            XmlElement v = node[ "original-value" ];
            if ( v != null )
            {
                XmlElement t = v[ "octetstring" ];
                if ( t != null )
                    ret.OriginalValue = DecodeUtils.DecodeBase64( t.InnerText );
            }

            return ret;
        }

        public static ObjectRefVariable BuildObjectRef ( XmlElement node )
        {
            ObjectRefVariable ret = new ObjectRefVariable();
            Build( node, ret );

            XmlElement v = node[ "original-value" ];
            if ( v != null )
            {
                XmlElement t = v["object-reference" ];
                ret.OriginalValue = ObjectRefBuilder.Build( t );
            }

            return ret;
        }

        public static ContentRefVariable BuildContentRef ( XmlElement node )
        {
            ContentRefVariable ret = new ContentRefVariable();
            Build( node, ret );

            XmlElement v = node[ "original-value" ];
            if ( v != null )
            {
                XmlElement t = v[ "content-reference" ];
                if ( t != null )
                    ret.OriginalValue = new ContentRef( DecodeUtils.DecodeBase64( t.InnerText ) );
            }

            return ret;
        }
    }

}
